from __future__ import with_statement

import socket

from lib.classloader import register
from lib.pocbase import  pocbase


class testpoc(pocbase):
    
    def __init__(self):        
        super(pocbase, self).__init__();
    
    def _match(self, infodict):
        port = infodict["port"];
        service_type = infodict["service_type"] if infodict.get("service_type") else "";
        version = infodict["service_version"] if infodict.get("service_version") else "";
        
        return int(port) == 6379 or version.lower().find("redis") != -1 or service_type.lower().find("redis") != -1;

    def _verify(self, infodict):
        host = infodict["ip"];
        port = infodict["port"];
        result = infodict;
        result["success"] = False;
        payload = '\x2a\x31\x0d\x0a\x24\x34\x0d\x0a\x69\x6e\x66\x6f\x0d\x0a'
        s = socket.socket()
        socket.setdefaulttimeout(10)
        try:
            host = host
            port = int(port)
            s.connect((host, port))
            s.send(payload)
            recvdata = s.recv(1024)
            if recvdata and 'redis_version' in recvdata:
                result['success'] = True
        except:
            pass
        return result;


register(__file__, testpoc)
